package com.smart.four.ioc.lifecycle.applicationcontext;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * BeanPostProcessor实现类
 * 对配置文件所提供的属性设置值进行处理
 *
 * @author junrui.wang
 * @create 2018/5/28
 */
public class MyBeanPostProcessor implements BeanPostProcessor {
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        //只对Car进行处理
        if("car".equalsIgnoreCase(beanName)){
            Car car = (Car) bean;
            if(car.getColor() == null){
                System.out.println("调用BeanPostProcessor.postProcessBeforeInitialization() run,color为空,设置默认颜色为黑色");
                car.setColor("黑色");
            }
        }
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        //只对Car进行处理
        if("car".equalsIgnoreCase(beanName)){
            Car car = (Car) bean;
            if(car.getMaxSpeed() >= 200){
                System.out.println("调用BeanPostProcessor.postProcessAfterInitialization() run, 将maxSpeed调整为200");
                car.setMaxSpeed(200);
            }
        }

        return bean;
    }
}
